package com.alibaba.citrus.cr.unified.inventory.functions.allocateApplyOrder;

import com.alibaba.citrus.cr.unified.inventory.utils.InfoUtil;
import com.alibaba.citrus.cr.unified.inventory.utils.OtsDateUtils;
import com.epoch.app.crunifiedinventory.model.dto.AllocateApplyOrderCreateDTO;
import com.epoch.app.crunifiedinventory.model.dto.AllocateApplyOrderDetailCreateDTO;
import com.epoch.app.crunifiedinventory.model.dto.AllocateApplyOrderReplyDTO;
import com.epoch.app.crunifiedinventory.model.dto.AllocateApplyOrderUpdateDTO;
import com.epoch.app.crunifiedinventory.sdo.AllocateApplyOrderDetailSDO;
import com.epoch.app.crunifiedinventory.sdo.AllocateApplyOrderSDO;
import com.epoch.app.crunifiedinventory.sdo.enums.AllocateApplyOrderStatusEnum;
import com.epoch.app.crunifiedinventory.sdo.enums.AllocateOrderTypeEnum;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class AllocateApplyOrderFactory {

    private static final String orderTime="16:00:00";
    private static final String HEADQUARTERS_SALES_ALLOCATION_ORDER_TIME="09:30:00";

    public static AllocateApplyOrderEntity create(AllocateApplyOrderCreateDTO allocateApplyOrderCreateDTO) {
        AllocateApplyOrderSDO allocateApplyOrderSDO = new AllocateApplyOrderSDO();
        BeanUtils.copyProperties(allocateApplyOrderCreateDTO,allocateApplyOrderSDO);
        allocateApplyOrderSDO.setShipWarehouseId(Objects.isNull(allocateApplyOrderCreateDTO.getShipWarehouseId())?0:Integer.valueOf(allocateApplyOrderCreateDTO.getShipWarehouseId()));
        allocateApplyOrderSDO.setShipCargoZoneId(Objects.isNull(allocateApplyOrderCreateDTO.getShipCargoZoneId())?0:Integer.valueOf(allocateApplyOrderCreateDTO.getShipCargoZoneId()));
        allocateApplyOrderSDO.setStatus(AllocateApplyOrderStatusEnum.DRAFT.getCode());
        //默认单据日期
        if(null == allocateApplyOrderCreateDTO.getOrderTime()||"".equals(allocateApplyOrderCreateDTO.getOrderTime())){
            Date date = new Date();
            String currentTime = OtsDateUtils.formatDate(date, OtsDateUtils.TIME_FORMAT);
            if (Objects.nonNull(allocateApplyOrderCreateDTO.getType())&&2 == allocateApplyOrderCreateDTO.getType()){
                if(currentTime.compareTo(HEADQUARTERS_SALES_ALLOCATION_ORDER_TIME)>0){
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.DAY_OF_MONTH,1);
                    date=calendar.getTime();
                }
            }else{
                if(currentTime.compareTo(orderTime)>0){
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.DAY_OF_MONTH,1);
                    date=calendar.getTime();
                }
            }
            allocateApplyOrderSDO.setOrderTime(OtsDateUtils.parseDate(OtsDateUtils.formatDate(date, OtsDateUtils.DATE_FORMAT),OtsDateUtils.DATE_FORMAT));
        }else {
            allocateApplyOrderSDO.setOrderTime(allocateApplyOrderCreateDTO.getOrderTime());
        }
        //生效日期
        if (Objects.isNull(allocateApplyOrderCreateDTO.getTakeEffectTime())||"".equals(allocateApplyOrderCreateDTO.getTakeEffectTime())){
            allocateApplyOrderSDO.setTakeEffectTime(allocateApplyOrderSDO.getOrderTime());
        }else{
            allocateApplyOrderSDO.setTakeEffectTime(allocateApplyOrderCreateDTO.getTakeEffectTime());
        }
        //失效日期
        if (Objects.isNull(allocateApplyOrderCreateDTO.getInvalidDate())||"".equals(allocateApplyOrderCreateDTO.getInvalidDate())){
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(allocateApplyOrderSDO.getTakeEffectTime());
            calendar.add(calendar.DATE,1);
            allocateApplyOrderSDO.setInvalidDate(calendar.getTime());
        }else{
            allocateApplyOrderSDO.setInvalidDate(allocateApplyOrderCreateDTO.getInvalidDate());
        }
        //运输方式
        allocateApplyOrderSDO.setShipMethod(allocateApplyOrderCreateDTO.getShipMethod());
        allocateApplyOrderSDO.setFromSys(allocateApplyOrderCreateDTO.getFromSys());
        allocateApplyOrderSDO.setBusinessType(allocateApplyOrderCreateDTO.getBusinessType());
        return AllocateApplyOrderEntity.of(allocateApplyOrderSDO);
    }

    public static AllocateApplyOrderEntity nykCreate(AllocateApplyOrderCreateDTO allocateApplyOrderCreateDTO) {
        AllocateApplyOrderSDO allocateApplyOrderSDO = new AllocateApplyOrderSDO();
        BeanUtils.copyProperties(allocateApplyOrderCreateDTO,allocateApplyOrderSDO);
        allocateApplyOrderSDO.setShipWarehouseId(Objects.isNull(allocateApplyOrderCreateDTO.getShipWarehouseId())?0:Integer.valueOf(allocateApplyOrderCreateDTO.getShipWarehouseId()));
        allocateApplyOrderSDO.setStatus(AllocateApplyOrderStatusEnum.COMPLETED.getCode());
        allocateApplyOrderSDO.setShipCargoZoneId(Objects.isNull(allocateApplyOrderCreateDTO.getShipCargoZoneId())?null:Integer.valueOf(allocateApplyOrderCreateDTO.getShipCargoZoneId()));
        //默认单据日期
        if(null == allocateApplyOrderCreateDTO.getOrderTime()||"".equals(allocateApplyOrderCreateDTO.getOrderTime())){
            Date date = new Date();
            String currentTime = OtsDateUtils.formatDate(date, OtsDateUtils.TIME_FORMAT);
            if(currentTime.compareTo(orderTime)>0){
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DAY_OF_MONTH,1);
                date=calendar.getTime();
            }
            allocateApplyOrderSDO.setOrderTime(OtsDateUtils.parseDate(OtsDateUtils.formatDate(date, OtsDateUtils.DATE_FORMAT),OtsDateUtils.DATE_FORMAT));
        }else {
            allocateApplyOrderSDO.setOrderTime(allocateApplyOrderCreateDTO.getOrderTime());
        }
        //生效日期
        if (Objects.isNull(allocateApplyOrderCreateDTO.getTakeEffectTime())||"".equals(allocateApplyOrderCreateDTO.getTakeEffectTime())){
            allocateApplyOrderSDO.setTakeEffectTime(allocateApplyOrderSDO.getOrderTime());
        }else{
            allocateApplyOrderSDO.setTakeEffectTime(allocateApplyOrderCreateDTO.getTakeEffectTime());
        }
        //失效日期
        if (Objects.isNull(allocateApplyOrderCreateDTO.getInvalidDate())||"".equals(allocateApplyOrderCreateDTO.getInvalidDate())){
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(allocateApplyOrderSDO.getTakeEffectTime());
            calendar.add(calendar.DATE,1);
            allocateApplyOrderSDO.setInvalidDate(calendar.getTime());
        }else{
            allocateApplyOrderSDO.setInvalidDate(allocateApplyOrderCreateDTO.getInvalidDate());
        }
        //运输方式
        allocateApplyOrderSDO.setShipMethod(allocateApplyOrderCreateDTO.getShipMethod());
        return AllocateApplyOrderEntity.of(allocateApplyOrderSDO);
    }

    public static AllocateApplyOrderEntity update(AllocateApplyOrderUpdateDTO allocateApplyOrderUpdateDTO) {
        AllocateApplyOrderSDO allocateApplyOrderSDO = covertSdo(allocateApplyOrderUpdateDTO);
        allocateApplyOrderSDO.setStatus(AllocateApplyOrderStatusEnum.DRAFT.getCode());
        allocateApplyOrderSDO.getList().forEach(
                allocateApplyOrderDetailSDO -> {
                    allocateApplyOrderDetailSDO.setStatus(AllocateApplyOrderStatusEnum.DRAFT.getCode());
                }
        );
        return AllocateApplyOrderEntity.of(allocateApplyOrderSDO);
    }

    public static AllocateApplyOrderEntity nykUpdate(AllocateApplyOrderUpdateDTO allocateApplyOrderUpdateDTO) {
        AllocateApplyOrderSDO allocateApplyOrderSDO = covertSdo(allocateApplyOrderUpdateDTO);
        allocateApplyOrderSDO.setStatus(AllocateApplyOrderStatusEnum.COMPLETED.getCode());
        allocateApplyOrderSDO.getList().forEach(
                allocateApplyOrderDetailSDO -> {
                    allocateApplyOrderDetailSDO.setStatus(AllocateApplyOrderStatusEnum.COMPLETED.getCode());
                }
        );
        return AllocateApplyOrderEntity.of(allocateApplyOrderSDO);
    }

    public static AllocateApplyOrderEntity copyDetail(List<AllocateApplyOrderDetailCreateDTO> allocateApplyOrderUpdateDTOs) {
        AllocateApplyOrderSDO allocateApplyOrderSDO = new AllocateApplyOrderSDO();
        List<AllocateApplyOrderDetailSDO> detailSDOList = allocateApplyOrderUpdateDTOs.stream().map(allocateApplyOrderDetailCreateDTO -> {
            AllocateApplyOrderDetailSDO allocateApplyOrderDetailSDO = new AllocateApplyOrderDetailSDO();
            BeanUtils.copyProperties(allocateApplyOrderDetailCreateDTO, allocateApplyOrderDetailSDO);
            return allocateApplyOrderDetailSDO;
        }).collect(Collectors.toList());
        allocateApplyOrderSDO.setList(detailSDOList);
        return AllocateApplyOrderEntity.of(allocateApplyOrderSDO);
    }
    public static AllocateApplyOrderEntity sapUpdate(AllocateApplyOrderUpdateDTO allocateApplyOrderUpdateDTO) {
        AllocateApplyOrderSDO allocateApplyOrderSDO = covertSdo(allocateApplyOrderUpdateDTO);
        for (AllocateApplyOrderDetailSDO allocateApplyOrderDetailSDO:allocateApplyOrderSDO.getList()){
            //根据编码获取商品id  sap回复数据,需要查找
            allocateApplyOrderDetailSDO.setGoodsId(InfoUtil.getScItemIdByOutCode(allocateApplyOrderDetailSDO.getScItemId()));
        }

        if (Objects.nonNull(allocateApplyOrderUpdateDTO)&& StringUtils.isNotEmpty(allocateApplyOrderUpdateDTO.getType())) {
            allocateApplyOrderSDO.setType(Integer.valueOf(allocateApplyOrderUpdateDTO.getType()));
        }
        return AllocateApplyOrderEntity.of(allocateApplyOrderSDO);
    }
    public static AllocateApplyOrderEntity cancel(AllocateApplyOrderUpdateDTO allocateApplyOrderUpdateDTO) {
        AllocateApplyOrderSDO allocateApplyOrderSDO = cancelCovertSdo(allocateApplyOrderUpdateDTO);
        allocateApplyOrderSDO.setStatus(AllocateApplyOrderStatusEnum.CANCELLED.getCode());
        return AllocateApplyOrderEntity.of(allocateApplyOrderSDO);
    }
    public static AllocateApplyOrderEntity reject(AllocateApplyOrderUpdateDTO allocateApplyOrderUpdateDTO) {
        AllocateApplyOrderSDO allocateApplyOrderSDO = covertSdo(allocateApplyOrderUpdateDTO);
        allocateApplyOrderSDO.setStatus(AllocateApplyOrderStatusEnum.CLOSED.getCode());
        return AllocateApplyOrderEntity.of(allocateApplyOrderSDO);
    }

    public static AllocateApplyOrderEntity submit(AllocateApplyOrderUpdateDTO allocateApplyOrderUpdateDTO) {
        AllocateApplyOrderSDO allocateApplyOrderSDO = covertSdo(allocateApplyOrderUpdateDTO);
        allocateApplyOrderSDO.setStatus(AllocateApplyOrderStatusEnum.WAIT_ANSWER.getCode());
        List<AllocateApplyOrderDetailSDO> list = allocateApplyOrderSDO.getList();
        for (AllocateApplyOrderDetailSDO allocateApplyOrderDetailSDO : list) {
            if (!(AllocateApplyOrderStatusEnum.COMPLETED.getCode().equals(allocateApplyOrderDetailSDO.getStatus())||AllocateApplyOrderStatusEnum.CLOSED.getCode().equals(allocateApplyOrderDetailSDO.getStatus()))) {
                allocateApplyOrderDetailSDO.setStatus(AllocateApplyOrderStatusEnum.WAIT_ANSWER.getCode());
            }
        }
        allocateApplyOrderSDO.setCommitTime(new Date());
        return AllocateApplyOrderEntity.of(allocateApplyOrderSDO);
    }
    public static AllocateApplyOrderEntity toBeSubmitted(AllocateApplyOrderUpdateDTO allocateApplyOrderUpdateDTO) {
        AllocateApplyOrderSDO allocateApplyOrderSDO = covertSdo(allocateApplyOrderUpdateDTO);
        allocateApplyOrderSDO.setStatus(AllocateApplyOrderStatusEnum.TO_BE_SUBMITTED.getCode());
        List<AllocateApplyOrderDetailSDO> list = allocateApplyOrderSDO.getList();
        for (AllocateApplyOrderDetailSDO allocateApplyOrderDetailSDO : list) {
            if (!(AllocateApplyOrderStatusEnum.COMPLETED.getCode().equals(allocateApplyOrderDetailSDO.getStatus())||AllocateApplyOrderStatusEnum.CLOSED.getCode().equals(allocateApplyOrderDetailSDO.getStatus()))) {
                allocateApplyOrderDetailSDO.setStatus(AllocateApplyOrderStatusEnum.TO_BE_SUBMITTED.getCode());
            }
        }
        allocateApplyOrderSDO.setCommitTime(new Date());
        return AllocateApplyOrderEntity.of(allocateApplyOrderSDO);
    }
    public static AllocateApplyOrderEntity headquartersSubmit(AllocateApplyOrderUpdateDTO allocateApplyOrderUpdateDTO) {
        AllocateApplyOrderSDO allocateApplyOrderSDO = covertSdo(allocateApplyOrderUpdateDTO);
        allocateApplyOrderSDO.setStatus(AllocateApplyOrderStatusEnum.HEADQUARTERS_REPLY.getCode());
        List<AllocateApplyOrderDetailSDO> list = allocateApplyOrderSDO.getList();
        for (AllocateApplyOrderDetailSDO allocateApplyOrderDetailSDO : list) {
            if (!(AllocateApplyOrderStatusEnum.COMPLETED.getCode().equals(allocateApplyOrderDetailSDO.getStatus())||AllocateApplyOrderStatusEnum.CLOSED.getCode().equals(allocateApplyOrderDetailSDO.getStatus()))) {
                allocateApplyOrderDetailSDO.setStatus(AllocateApplyOrderStatusEnum.HEADQUARTERS_REPLY.getCode());
            }
        }
        if (Objects.isNull(allocateApplyOrderSDO.getCommitTime())){
            allocateApplyOrderSDO.setCommitTime(new Date());
        }
        return AllocateApplyOrderEntity.of(allocateApplyOrderSDO);
    }
    public static AllocateApplyOrderEntity shipperSubmit(AllocateApplyOrderUpdateDTO allocateApplyOrderUpdateDTO) {
        AllocateApplyOrderSDO allocateApplyOrderSDO = covertSdo(allocateApplyOrderUpdateDTO);
        allocateApplyOrderSDO.setStatus(AllocateApplyOrderStatusEnum.SHIPPER_REPLY.getCode());
        List<AllocateApplyOrderDetailSDO> list = allocateApplyOrderSDO.getList();
        for (AllocateApplyOrderDetailSDO allocateApplyOrderDetailSDO : list) {
            if (!(AllocateApplyOrderStatusEnum.COMPLETED.getCode().equals(allocateApplyOrderDetailSDO.getStatus())||AllocateApplyOrderStatusEnum.CLOSED.getCode().equals(allocateApplyOrderDetailSDO.getStatus()))) {
                allocateApplyOrderDetailSDO.setStatus(AllocateApplyOrderStatusEnum.SHIPPER_REPLY.getCode());
            }
        }
        return AllocateApplyOrderEntity.of(allocateApplyOrderSDO);
    }
    public static AllocateApplyOrderEntity reply(AllocateApplyOrderReplyDTO allocateApplyOrderReplyDTO) {
        AllocateApplyOrderSDO allocateApplyOrderSDO = new AllocateApplyOrderSDO();
        BeanUtils.copyProperties(allocateApplyOrderReplyDTO,allocateApplyOrderSDO);
        //回复时间
        allocateApplyOrderSDO.setConfirmTime(new Date());
        List<AllocateApplyOrderDetailCreateDTO> list = allocateApplyOrderReplyDTO.getList();
        List<AllocateApplyOrderDetailSDO> detailSDOList = Lists.newArrayList();
        for (AllocateApplyOrderDetailCreateDTO applyOrderDetailCreateDTO : list) {
            AllocateApplyOrderDetailSDO allocateApplyOrderDetailSDO = new AllocateApplyOrderDetailSDO();
            BeanUtils.copyProperties(applyOrderDetailCreateDTO, allocateApplyOrderDetailSDO);
            if(null==allocateApplyOrderDetailSDO.getStatus()){
                allocateApplyOrderDetailSDO.setStatus(AllocateApplyOrderStatusEnum.PART_ANSWER.getCode());
            }
            allocateApplyOrderDetailSDO.setAllocateApplyOrderId(allocateApplyOrderSDO.getId().intValue());
            //提报数量
            Integer applyQuantity = applyOrderDetailCreateDTO.getApplyQuantity();
            applyQuantity = Objects.isNull(applyQuantity)?0:applyQuantity;
            //已回复数量
            Integer replyQuantity = applyOrderDetailCreateDTO.getReplyQuantity();
            if(null==replyQuantity){
                replyQuantity=0;
            }
            //发货数量
            Integer deliverQuantity = applyOrderDetailCreateDTO.getDeliverQuantity();
            deliverQuantity = Objects.isNull(deliverQuantity)?0:deliverQuantity;
            if(deliverQuantity>0&&applyQuantity-replyQuantity>=deliverQuantity){
                //判断如果行状态完成或者取消就不更改已回复数量
                if (!(AllocateApplyOrderStatusEnum.CLOSED.getCode().equals(allocateApplyOrderDetailSDO.getStatus())
                        ||AllocateApplyOrderStatusEnum.COMPLETED.getCode().equals(allocateApplyOrderDetailSDO.getStatus()))) {
                    if (Objects.isNull(allocateApplyOrderDetailSDO.getIsClosed()) || 1!=allocateApplyOrderDetailSDO.getIsClosed()) {
                        allocateApplyOrderDetailSDO.setReplyQuantity(deliverQuantity + replyQuantity);
                    }
                }
            }
            if(Objects.equals(allocateApplyOrderDetailSDO.getReplyQuantity(), applyQuantity)){
                allocateApplyOrderDetailSDO.setStatus(AllocateApplyOrderStatusEnum.COMPLETED.getCode());
            }
            //判断明细是否关闭
            if (Objects.nonNull(allocateApplyOrderDetailSDO.getIsClosed())&&1==allocateApplyOrderDetailSDO.getIsClosed()){
                allocateApplyOrderDetailSDO.setStatus(AllocateApplyOrderStatusEnum.CLOSED.getCode());
            }
            detailSDOList.add(allocateApplyOrderDetailSDO);
        }

        allocateApplyOrderSDO.setStatus(AllocateApplyOrderStatusEnum.COMPLETED.getCode());
        //判断主单是否全部关闭，没关闭修改状态
        boolean isUpdateStatus = true;
        for (AllocateApplyOrderDetailSDO allocateApplyOrderDetailSDO:detailSDOList){
            if(!allocateApplyOrderDetailSDO.getStatus().equals(AllocateApplyOrderStatusEnum.CLOSED.getCode())){
                //判断明细中是否有未回复完的预报单明细，如果有修改状态为部分回复或总部回复，修改后不在为这个值做修改
                if (!(AllocateApplyOrderStatusEnum.COMPLETED.getCode().equals(allocateApplyOrderDetailSDO.getStatus())||AllocateApplyOrderStatusEnum.CLOSED.getCode().equals(allocateApplyOrderDetailSDO.getStatus()))){
                    //判断预报单是借货的类型，修改明细回复状态
                    if (AllocateOrderTypeEnum.CROSS_COMPANY.getType().equals(Integer.valueOf(allocateApplyOrderReplyDTO.getType()))){
                        if (isUpdateStatus){
                            allocateApplyOrderSDO.setStatus(AllocateApplyOrderStatusEnum.HEADQUARTERS_REPLY.getCode());
                        }
                        allocateApplyOrderDetailSDO.setStatus(AllocateApplyOrderStatusEnum.HEADQUARTERS_REPLY.getCode());
                    }else{
                        if (isUpdateStatus){
                            allocateApplyOrderSDO.setStatus(AllocateApplyOrderStatusEnum.PART_ANSWER.getCode());
                        }
                        allocateApplyOrderDetailSDO.setStatus(AllocateApplyOrderStatusEnum.PART_ANSWER.getCode());
                    }
                    isUpdateStatus = false;
                    allocateApplyOrderDetailSDO.setReplyDate(new Date());
                }
            }
        }
        //完成时间
        if (AllocateApplyOrderStatusEnum.COMPLETED.getCode().equals(allocateApplyOrderSDO.getStatus())){
            allocateApplyOrderSDO.setCompleteTime(new Date());
        }
        allocateApplyOrderSDO.setList(detailSDOList);
        return AllocateApplyOrderEntity.of(allocateApplyOrderSDO);
    }

    private static AllocateApplyOrderSDO covertSdo(AllocateApplyOrderUpdateDTO allocateApplyOrderUpdateDTO){
        AllocateApplyOrderSDO allocateApplyOrderSDO = new AllocateApplyOrderSDO();
        BeanUtils.copyProperties(allocateApplyOrderUpdateDTO,allocateApplyOrderSDO);
        if (Objects.nonNull(allocateApplyOrderUpdateDTO.getShipWarehouseId())){
            allocateApplyOrderSDO.setShipWarehouseId(Integer.valueOf(allocateApplyOrderUpdateDTO.getShipWarehouseId()));
        }
        if (Objects.nonNull(allocateApplyOrderUpdateDTO.getShipCargoZoneId())){
            allocateApplyOrderSDO.setShipCargoZoneId(Integer.valueOf(allocateApplyOrderUpdateDTO.getShipCargoZoneId()));
        }
        List<AllocateApplyOrderDetailCreateDTO> list = allocateApplyOrderUpdateDTO.getList();
        if(CollectionUtils.isEmpty(list)){
            throw new IllegalArgumentException("货品详情不能为空");
        }
        List<AllocateApplyOrderDetailSDO> detailSDOList = list.stream().map(allocateApplyOrderDetailCreateDTO -> {
            AllocateApplyOrderDetailSDO allocateApplyOrderDetailSDO = new AllocateApplyOrderDetailSDO();
            BeanUtils.copyProperties(allocateApplyOrderDetailCreateDTO, allocateApplyOrderDetailSDO);
            allocateApplyOrderDetailSDO.setAllocateApplyOrderId(allocateApplyOrderSDO.getId().intValue());
            return allocateApplyOrderDetailSDO;
        }).collect(Collectors.toList());
        allocateApplyOrderSDO.setList(detailSDOList);
        return allocateApplyOrderSDO;
    }

    private static AllocateApplyOrderSDO cancelCovertSdo(AllocateApplyOrderUpdateDTO allocateApplyOrderUpdateDTO){
        AllocateApplyOrderSDO allocateApplyOrderSDO = new AllocateApplyOrderSDO();
        BeanUtils.copyProperties(allocateApplyOrderUpdateDTO,allocateApplyOrderSDO);
        List<AllocateApplyOrderDetailCreateDTO> list = allocateApplyOrderUpdateDTO.getList();
        if (Objects.nonNull(list)){
            List<AllocateApplyOrderDetailSDO> detailSDOList = list.stream().map(allocateApplyOrderDetailCreateDTO -> {
                AllocateApplyOrderDetailSDO allocateApplyOrderDetailSDO = new AllocateApplyOrderDetailSDO();
                BeanUtils.copyProperties(allocateApplyOrderDetailCreateDTO, allocateApplyOrderDetailSDO);
                allocateApplyOrderDetailSDO.setAllocateApplyOrderId(allocateApplyOrderSDO.getId().intValue());
                return allocateApplyOrderDetailSDO;
            }).collect(Collectors.toList());
            allocateApplyOrderSDO.setList(detailSDOList);
        }
        return allocateApplyOrderSDO;
    }
}
